package com.jianyun.jyzs.utils;

import com.jianyun.jyzs.bean.User;
import com.jianyun.jyzs.constant.SysConstant;

import org.json.JSONArray;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by yangshijun on 2018/9/6.
 */

public class JSONObjectUtil {
    /*
         * object转通讯录
         * object
         * userId 当前登录用户的serverUserId
         */
    public static List<User> object2Users(JSONObject object, String userId) throws Exception {
        List<User> users = new ArrayList<User>();
        User user = null;
        JSONObject obj = null;
        JSONArray userList = object.getJSONArray("list");//获取JSONArray
        for (int i = 0; i < userList.length(); i++) {//遍历JSONArray
            obj = userList.getJSONObject(i);
            user = new User();
            user.setServerUserId(obj.getString("id")); // 用户ID
            user.setName(obj.getString("userName")); // 账号
            user.setNameZh(obj.getString("name")); // 用户名称
            user.setEmail(obj.getString("email")); // 邮箱
            user.setPhoneNum(obj.getString("phoneNum"));//手机号
            user.setCompany(obj.getString("company"));//所在单位
            user.setDepartment(obj.getString("department"));//所在部门
            user.setSex(obj.getString("sex"));//性别
            user.setServerIconPath(obj.getString("iconPaths")); // 头像URL
            //user.setUserType(SysConstant.YES); // 用户类型,0:登录用户;1:朋友
            user.setUserId(userId);
            try {
                user.setIsCommonContact(EmptyHandle.handleInt(obj.get("isContact")));
            } catch (Exception e) {
            }
            users.add(user);
        }
        return users;
    }

    /*
	 * object转用户
	 * object
	 * userId 当前登录用户的serverUserId
	 */
    public static User object2User(JSONObject obj) throws Exception {
        User user = new User();
        user.setServerUserId(obj.getString("id")); // 用户ID
        user.setName(obj.getString("userName")); // 账号
        user.setNameZh(obj.getString("name")); // 用户名称
        user.setEmail(obj.getString("email")); // 邮箱
        user.setPhoneNum(obj.getString("phoneNum"));//手机号
        user.setCompany(obj.getString("company"));//所在单位
        user.setDepartment(obj.getString("department"));//所在部门
        user.setSex(obj.getString("sex"));//性别
        user.setServerIconPath(obj.getString("iconPaths")); // 头像URL
        return user;
    }
}
